package com.lzp.service.impl;

import com.lzp.mapper.UserAddressMapper;
import com.lzp.pojo.UserAddress;
import com.lzp.pojo.bo.AddressBO;
import com.lzp.service.AddressService;
import com.lzp.utils.enums.Yes0rNo;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 收货地址业务逻辑
 *
 * @author dtyy
 * @date 2020/8/27
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private Sid sid;

    /**
     * 根据用户id查询收货地址列表
     *
     * @param userId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<UserAddress> queryAllAddressByUserId(String userId) {
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);

        return userAddressMapper.select(userAddress);
    }

    /**
     * 新增地址
     *
     * @param addressBO
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void addNewUserAddress(AddressBO addressBO) {
        // 查询是否是首次添加地址
        Integer isDefault = 0;
        List<UserAddress> list = queryAllAddressByUserId(addressBO.getUserId());
        if (list == null || list.isEmpty()) {
            isDefault = 1;
        }

        // 持久化到数据库
        UserAddress newAddress = new UserAddress();
        BeanUtils.copyProperties(addressBO, newAddress);
        newAddress.setId(sid.nextShort());
        newAddress.setIsDefault(isDefault);
        newAddress.setCreatedTime(new Date());
        newAddress.setUpdatedTime(new Date());

        userAddressMapper.insert(newAddress);
    }

    /**
     * 更新地址
     *
     * @param addressBO
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateUserAddress(AddressBO addressBO) {
        UserAddress address = new UserAddress();
        BeanUtils.copyProperties(addressBO, address);
        address.setId(addressBO.getAddressId());
        address.setUpdatedTime(new Date());

        userAddressMapper.updateByPrimaryKeySelective(address);
    }

    /**
     * 删除地址
     *
     * @param userId
     * @param addressId
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteUserAddress(String userId, String addressId) {
        UserAddress address = new UserAddress();
        address.setUserId(userId);
        address.setId(addressId);

        userAddressMapper.delete(address);
    }

    /**
     * 设置默认地址
     *
     * @param userId
     * @param addressId
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateUserAddressToBeDefault(String userId, String addressId) {
        // 将原默认地址设为非默认
        UserAddress queryAddress = new UserAddress();
        queryAddress.setUserId(userId);
        queryAddress.setIsDefault(Yes0rNo.YES.getCode());
        List<UserAddress> list = userAddressMapper.select(queryAddress);
        for (UserAddress ua : list) {
            ua.setIsDefault(Yes0rNo.NO.getCode());
            userAddressMapper.updateByPrimaryKeySelective(ua);
        }

        // 设置目标地址为默认地址
        UserAddress address = new UserAddress();
        address.setId(addressId);
        address.setUserId(userId);
        address.setIsDefault(Yes0rNo.YES.getCode());
        address.setUpdatedTime(new Date());
        userAddressMapper.updateByPrimaryKeySelective(address);
    }

    /**
     * 根据用户id和地址id，查询用户地址对象信息
     *
     * @param userId
     * @param addressId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public UserAddress queryUserAddress(String userId, String addressId) {
        UserAddress address = new UserAddress();
        address.setUserId(userId);
        address.setId(addressId);

        return userAddressMapper.selectOne(address);
    }

}
